package com.hrg.rds.biz.job;


import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.hrg.rds.biz.comm.NavService;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.rds.biz.robot.domain.AgvVo;
import com.hrg.rds.biz.traffic.TrafficManagement;
import com.hrg.rds.biz.waybill.domain.template.TemplateParam;
import com.hrg.rds.biz.waybill.service.WaybillInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 把这个逻辑加锁，和发起运单实例共享一把锁
 */
@Component("HrgJob")
public class TJob {

	@Autowired
	private WaybillInstanceService waybillInstanceService;
	private final Map<String, AgvVo> AgvIpMap = RdsConfiguration.AgvIpMap;

	public void auto() {
		// 释放交管资源
		TrafficManagement.refreshTrafficControl();
		// 生成待命点或充电任务
		// 判断车辆空闲时长，默认超过10s，生成待命点或充电任务
		List<AgvVo> agvVoList = AgvIpMap.values().stream().filter(AgvVo::isIfAvailable4Auto).toList();
		for (AgvVo agvVo : agvVoList) {
			fireAutoWaybillInstance(agvVo, goStandByCheck(agvVo), goChargeCheck(agvVo));
		}
	}

	public boolean goStandByCheck(AgvVo agvVo) {
		// 生成待命点任务
		// 是否开启自动停靠功能 AutoPark
		// 任务完成后去自动停靠任务的时间 StartParkTime 10s (0, 100)
		if (agvVo.getIdleStartedTime() != null && DateUtil.between(agvVo.getIdleStartedTime(), new Date(), DateUnit.SECOND) > 10) {
			// 空闲10s
			// 不在待命点且不在充电点，或者在充电点且充电完成
			if ((!agvVo.getStandbyStation().equals(agvVo.getCurrentStation()) && !agvVo.getChargeStation().equals(agvVo.getCurrentStation()))
					|| (agvVo.getChargeStation().equals(agvVo.getCurrentStation())) && agvVo.getIfCharging() && NumberUtil.compare(agvVo.getBatteryLevel(), 99) >= 0) {
				return true;
			}

		}
		return false;
	}

	public boolean goChargeCheck(AgvVo agvVo) {
		// 生成充电任务
		// 是否开启自动充电功能 AutoCharge
		// 任务完成后去自动充电任务的时间 StartChargeTime 10s (0, 100)
		if (agvVo.getIdleStartedTime() != null && DateUtil.between(agvVo.getIdleStartedTime(), new Date(), DateUnit.SECOND) > 10) {
			// 空闲10s
			// 电量低于60，且不在充电点充电中
			if (!agvVo.getIfCharging() && !agvVo.getChargeStation().equals(agvVo.getCurrentStation()) && NumberUtil.compare(agvVo.getBatteryLevel(), 60) < 0) {
				return true;
			}

		}
		return false;
	}

	public void fireAutoWaybillInstance(AgvVo agvVo, boolean isCharge, boolean isStandby) {
		// 系统默认配置回充电点和待命点的模板
		// 生成回待命点和充电点运单任务
		String agvIp = agvVo.getIp();
		String currentStation = agvVo.getCurrentStation();
		String targetStation = null, waybillTemplateId = "0";
		if (isStandby) {
			targetStation = agvVo.getStandbyStation();
			// 待命点任务
			//waybillTemplateId = "";
		}
		if (isCharge) {
			targetStation = agvVo.getChargeStation();
			// 充电点任务
			//waybillTemplateId = "";
		}
		// 两个同时满足，优先充电
		if (isCharge || isStandby) {
			List<String> lockPaths = NavService.getNavPath(agvIp, currentStation, targetStation);
			if (lockPaths != null && !lockPaths.isEmpty()) {
				if (TrafficManagement.tryTrafficControl(lockPaths, agvIp, lockPaths)) {
					// 导航站点、执行车辆
					List<TemplateParam> templateParams = new ArrayList<>();
					TemplateParam agvIdParam = new TemplateParam();
					agvIdParam.setName("agvId");
					agvIdParam.setValue(agvVo.getId());
					agvIdParam.setRequired(true);
					templateParams.add(agvIdParam);
					TemplateParam targetStationParam = new TemplateParam();
					targetStationParam.setName("targetStation");
					targetStationParam.setValue(targetStation);
					targetStationParam.setRequired(true);
					templateParams.add(agvIdParam);
					waybillInstanceService.fireInstance(waybillTemplateId, templateParams, null);
				}
			}
		}
	}
}
